package es.ceura.wilder.parsers;

import es.ceura.wilder.datastructures.Cookie;
import es.ceura.wilder.datastructures.Header;
import es.ceura.wilder.datastructures.HeaderType;
import es.ceura.wilder.datastructures.request.HTTPRequest;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author 66785403 (Jorge Madrid Portillo)
 */
public class CookieParser {

    public CookieParser() {
    }

    public Set<Cookie> getCookies(HTTPRequest request) {
        HashSet<Cookie> cookieSet = new HashSet<>();

        Header cookieHeader = request.getHeaderValue(HeaderType.COOKIE);
        if (cookieHeader != null) {
            String[] cookieEntries = cookieHeader.getValue().split(";");
            for (String cookieEntry : cookieEntries) {
                String[] splitCookie = cookieEntry.split("=");
                Cookie cookie = new Cookie(splitCookie[0], splitCookie[1]);
                cookieSet.add(cookie);
            }
        }
        return cookieSet;
    }
}
